<?php
/**
 *
 * @package avp
 * @version $Id: $
 * @copyright (c) 2006 Jan-Willem van Ganswijk, Stefan van Essen
 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
 */
define( 'IN_SCRIPT', 'TRUE' ); //Voor de evil haxx0rz.
$root = '../';

include( $root . 'includes/common.inc.php' );
include( $root . 'includes/lang.inc.php' );
include( $root . 'includes/session.class.php' );
include( $root . 'includes/admin.inc.php' );
include( $root . 'includes/template.class.php' );

$session = new sessionWrapper();

$lang = read_langfiles( 'admin_common,admin_plugins' );
$template = new template( $CONFIG['theme'] );
$template->add_var('active', 'plugins');


if( !$session->check_login() ) {
    die( print_error( $lang['ERROR_NEEDLOGIN'], 'login.php', true ) );
}

$action = addslashes( $_GET['action'] );


if( $action == 'list' || empty( $action ) ) {
    $result = $db->query( "SELECT * FROM `" . $db->get_table( 'plugins' ) . "`" );

    $installed_plugin_dirs = array();
    while( $row = $db->fetch( $result ) ) {
        $template->append( 'installed_plugins', $row );
        $installed_plugin_dirs[] = $row['plugindir'];
    }

    $not_installed_plugins = array();
    if ( $dir_handle = @opendir( $root . 'plugins' ) ) {
        while ( $file = readdir( $dir_handle ) ) {
            if ( $file == '.' || $file == '..' ) {
                continue;
            }
            if ( !in_array( $file, $installed_plugin_dirs ) && is_readable( $root . 'plugins/' . $file . '/plugin.cfg.php' ) ) {
                $plugin = array(); //Clear previous
                require( $root . 'plugins/' . $file . '/plugin.cfg.php' );
                $plugin['dirname'] = $file;
                $not_installed_plugins[] = $plugin;
            }
        }

        closedir( $dir_handle );
    }
    $template->add_vars( array( 'not_installed_plugins' => $not_installed_plugins ) );
    $template->set_title( $lang['TITLE_PLUGINS_LIST'] );
    $template->add_templates( 'admin_header,admin_plugins_list,admin_footer' );
    $template->make_page();
    exit;
} elseif( $action == 'install' ) {
    $dir = addslashes( $_GET['dir'] );
    if( $dir < 0 || !isset( $_GET['dir'] ) ) {
        print_error( $lang['ERROR_NOID'], $_SERVER['PHP_SELF'] );
        exit;
    }

    if(is_readable( $root . 'plugins/' . $dir . '/plugin.cfg.php' )) {
		require($root . 'plugins/' . $dir . '/plugin.cfg.php');
		$db->query("INSERT INTO `" . $db->get_table( 'plugins' ) . "` (`name`,`description`,`plugindir`,`imagescript`,`configscript`,`arguments`) VALUES ('{$plugin['name']}','{$plugin['description']}','{$dir}','{$plugin['imagescript']}','{$plugin['configscript']}','{$plugin['default_arguments']}')");
		print_message( $lang['TITLE_INSTALL_PLUGIN'], $lang['MSG_INSTALLED'], $_SERVER['PHP_SELF'] );
		exit;
	} else {
		print_error( $lang['ERROR_PLUGIN_NOT_FOUND'], $_SERVER['PHP_SELF'] );
		exit;
	}

} elseif( $action == 'uninstall' ) {
    $id = addslashes( $_GET['id'] );
    if( $id < 0 || !isset( $_GET['id'] ) ) {
        print_error( $lang['ERROR_NOID'], $_SERVER['PHP_SELF'] );
        exit;
    }
    if( isset( $_GET['submit'] ) ) {
        $db->query( "DELETE FROM `" . $db->get_table( 'plugins' ) . "` WHERE `pluginid`='{$id}' LIMIT 1" );

        print_message( $lang['TITLE_UNINSTALL_PLUGIN'], $lang['MSG_UNINSTALLED'], $_SERVER['PHP_SELF'] );
        exit;
    }

    print_confirmbox( $_SERVER['PHP_SELF'] . "?action=uninstall&submit&id=" . $id, $_SERVER['PHP_SELF'] , $lang['TITLE_UNINSTALL_PLUGIN'] );
    exit;
} elseif( $action == 'configure' ) {
    $id = addslashes( $_GET['id'] );
    if( $id < 0 || !isset( $_GET['id'] ) ) {
        print_error( $lang['ERROR_NOID'], $_SERVER['PHP_SELF'] );
        exit;
    }
    $plugininfo = $db->query_first("SELECT * FROM `" . $db->get_table( 'plugins' ) . "` WHERE `pluginid`='{$id}' LIMIT 1");
}

?>
